/**
 * Copyright 2000 self.com.au
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. (http://www.apache.org/licenses/LICENSE-2.0)
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed 
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for 
 * the specific language governing permissions and limitations under the License.
 *
 * <dhamma-message src="Atisha (11th century Tibetan Buddhist master)">
 * 	The greatest achievement is selflessness. The greatest worth is self-mastery.
 * 	The greatest quality is seeking to serve others. The greatest precept is continual awareness.
 * 	The greatest medicine is the emptiness of everything. The greatest action is not conforming with the worlds ways.
 * 	The greatest magic is transmuting the passions. The greatest generosity is non-attachment.
 * 	The greatest goodness is a peaceful mind. The greatest patience is humility.
 * 	The greatest effort is not concerned with results. The greatest meditation is a mind that lets go.
 * 	The greatest wisdom is seeing through appearances. 
 * </dhamma-message>
 *
 * @author Ashin Wimalajeewa
 */
package self.amigo.editor.cmd;

import java.awt.Point;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import self.gee.IGraphicElement;
import self.gee.IPointSeriesElement;
import self.gee.IRectangularElement;
import self.gee.editor.EventManager;
import self.gee.editor.ICommand;
import self.gee.editor.IDiagramEditor;

public class SelectionMovement implements ICommand {
  private IDiagramEditor  editor;
  private EventManager    controller;
  //private MouseEvent      eventStart, eventEnd;
  private java.util.List  selection = new ArrayList();
  private Map             pointSeriesMap = new HashMap();
  private Map             rectangularMap = new HashMap();
  
  public SelectionMovement( IDiagramEditor ed, EventManager ctl, MouseEvent e, java.util.List s ) {
    editor = ed;
    controller = ctl;
    //eventStart = e;
    selection.addAll( s );
    
    int max = selection.size();
    for ( int cntr = 0; cntr < max; cntr++ ) {
      IGraphicElement elem = (IGraphicElement) selection.get( cntr );
      Integer id = new Integer( elem.getReferenceId() );
      if ( elem instanceof IPointSeriesElement ) {
        PointSeriesMovementEntry entry = new PointSeriesMovementEntry();
        pointSeriesMap.put( id, entry );
        initPointSeries( elem, entry.original );
      } else if ( elem instanceof IRectangularElement ) {
        RectangleMovementEntry entry = new RectangleMovementEntry();
        rectangularMap.put( id, entry );
        initRectangle( elem, entry.original );
      }
    }
  }
  
  public void endMovement( MouseEvent e ) {
    //eventEnd = e;
    int max = selection.size();
    for ( int cntr = 0; cntr < max; cntr++ ) {
      IGraphicElement elem = (IGraphicElement) selection.get( cntr );
      Integer id = new Integer( elem.getReferenceId() );
      if ( elem instanceof IPointSeriesElement ) {
        PointSeriesMovementEntry entry = (PointSeriesMovementEntry) pointSeriesMap.get( id );
        initPointSeries( elem, entry.current );
      } else if ( elem instanceof IRectangularElement ) {
        RectangleMovementEntry entry = (RectangleMovementEntry) rectangularMap.get( id );
        initRectangle( elem, entry.current );
      }
    }
  }
  
  public void execute() {
    final List refreshedElements = SelectionUtils.toNewElementList( selection, controller.diagramLayer );
    final int max = refreshedElements.size();
    controller.diagramLayer.beginUpdate();
    for ( int cntr = 0; cntr < max; cntr++ ) {
      IGraphicElement elem = (IGraphicElement) refreshedElements.get( cntr );
      Integer id = new Integer( elem.getReferenceId() );
      if ( elem instanceof IPointSeriesElement ) {
        PointSeriesMovementEntry entry = (PointSeriesMovementEntry) pointSeriesMap.get( id );
        ((IPointSeriesElement) elem).setFramePoints( entry.current );
      } else {
        RectangleMovementEntry entry = (RectangleMovementEntry) rectangularMap.get( id );
        elem.setLocation( entry.current.x, entry.current.y );
//        elem.move( offset, eventEnd, IGraphicElement.DS_AFTER_FIRST );
//        elem.move( offset, eventEnd, IGraphicElement.DS_AFTER_LAST );
      }
    }
    controller.diagramLayer.endUpdate();
    editor.setSelectedElements( refreshedElements );
    editor.getSource().redraw();
  }
  
  public void undo() {
    final List refreshedElements = SelectionUtils.toNewElementList( selection, controller.diagramLayer );
    final int max = refreshedElements.size();
    controller.diagramLayer.beginUpdate();
    for ( int cntr = max-1; cntr >= 0; cntr-- ) {
      IGraphicElement elem = (IGraphicElement) refreshedElements.get( cntr );
      Integer id = new Integer( elem.getReferenceId() );
      if ( elem instanceof IPointSeriesElement ) {
        PointSeriesMovementEntry entry = (PointSeriesMovementEntry) pointSeriesMap.get( id );
        ((IPointSeriesElement) elem).setFramePoints( entry.original );
      } else {
        RectangleMovementEntry entry = (RectangleMovementEntry) rectangularMap.get( id );
        elem.setLocation( entry.original.x, entry.original.y );
//        elem.move( inverse, eventEnd, IGraphicElement.DS_AFTER_FIRST );
//        elem.move( inverse, eventEnd, IGraphicElement.DS_AFTER_LAST );
      }
    }
    controller.diagramLayer.endUpdate();
    editor.setSelectedElements( refreshedElements );
    editor.getSource().redraw();
  }
  
//  private void initOriginalPointSeries(

  static private class PointSeriesMovementEntry {
    public List original = new ArrayList();
    public List current = new ArrayList();
  }
  
  static private class RectangleMovementEntry {
    public Point original = new Point();
    public Point current = new Point();
  }
  
  static private void initPointSeries( Object element, List store ) {
    if ( element instanceof IPointSeriesElement ) {
      ((IPointSeriesElement) element).getFramePoints( store );
      int max = store.size();
      for ( int cntr = 0; cntr < max; cntr++ ) 
        store.set( cntr, new Point((Point)store.get(cntr)) );
    }
  }
  
  static private void initRectangle( Object element, Point p ) {
    if ( element instanceof IRectangularElement ) 
      p.setLocation( ((IRectangularElement)element).getBounds().getLocation() );
  }
}